﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YJKExcelFormTool.DataDefine;
using YJKExcelFormTool.Utilities;

namespace YJKExcelFormTool.Creator
{
	public class DoorWindowDetailedStatementCreator : TableCreator
	{
		public DoorWindowDetailedStatementCreator(ExternalCommandData cmdData)
		{
			this.m_Revit = cmdData;
		}

		public TableBody CreateTableBody(List<TableHeadCellKey> headerParameters)
		{
			List<TableBodyRow> list = new List<TableBodyRow>();
			List<DoorWindowDetailedStatementCreator.DoorWindowTypeInfos> typeInfoByCategory = this.GetTypeInfoByCategory((BuiltInCategory)(-2000023));
			int num = 1;
			foreach (DoorWindowDetailedStatementCreator.DoorWindowTypeInfos typeInfos in typeInfoByCategory)
			{
				List<TableBodyRow> collection = this.CreateDataRecord(num, typeInfos, headerParameters);
				list.AddRange(collection);
				num++;
			}
			foreach (DoorWindowDetailedStatementCreator.DoorWindowTypeInfos typeInfos2 in this.GetTypeInfoByCategory((BuiltInCategory)(-2000014)))
			{
				List<TableBodyRow> collection2 = this.CreateDataRecord(num, typeInfos2, headerParameters);
				list.AddRange(collection2);
				num++;
			}
			return new TableBody(list);
		}

		private List<DoorWindowDetailedStatementCreator.DoorWindowTypeInfos> GetTypeInfoByCategory(BuiltInCategory category)
		{
			List<DoorWindowDetailedStatementCreator.DoorWindowTypeInfos> list = new List<DoorWindowDetailedStatementCreator.DoorWindowTypeInfos>();
			foreach (Element element in new FilteredElementCollector(this.m_Revit.Application.ActiveUIDocument.Document).OfCategory(category))
			{
				FamilyInstance familyInstance = element as FamilyInstance;
				if (familyInstance != null)
				{
					ElementId typeId = familyInstance.GetTypeId();
					string name = familyInstance.GetYJKLevel().Name;
					double elevation = familyInstance.GetYJKLevel().Elevation;
					int num = list.FindIndex((DoorWindowDetailedStatementCreator.DoorWindowTypeInfos s) => s.typeId.IntegerValue == typeId.IntegerValue);
					if (num >= 0)
					{
						list[num].AddInfos(name, elevation);
					}
					else
					{
						DoorWindowDetailedStatementCreator.DoorWindowTypeInfos doorWindowTypeInfos = new DoorWindowDetailedStatementCreator.DoorWindowTypeInfos();
						doorWindowTypeInfos.typeId = typeId;
						doorWindowTypeInfos.infosListByLevel = new List<DoorWindowDetailedStatementCreator.DoorWindowTypeInfosByLevel>();
						doorWindowTypeInfos.AddInfos(name, elevation);
						list.Add(doorWindowTypeInfos);
					}
				}
			}
			return list;
		}

		private List<TableBodyRow> CreateDataRecord(int index, DoorWindowDetailedStatementCreator.DoorWindowTypeInfos typeInfos, List<TableHeadCellKey> headerParameters)
		{
			Document document = this.m_Revit.Application.ActiveUIDocument.Document;
			List<TableBodyRow> list = new List<TableBodyRow>();
			for (int i = 0; i < typeInfos.infosListByLevel.Count; i++)
			{
				FamilySymbol familySymbol = RevitVersionFuncs.GetElement(document, typeInfos.typeId) as FamilySymbol;
				if (familySymbol != null)
				{
					List<TableBodyCell> list2 = new List<TableBodyCell>();
					foreach (TableHeadCellKey tableHeadCellKey in headerParameters)
					{
						string text = tableHeadCellKey.KeyName;
						if (text.CompareTo("族名") == 0)
						{
							string name = familySymbol.Family.Name;
							TableBodyCell item = new TableBodyCell(TableBodyCell._tagDataType._DT_TEXT, name);
							list2.Add(item);
						}
						else if (text.CompareTo("规格") == 0)
						{
							string name2 = familySymbol.Name;
							TableBodyCell item = new TableBodyCell(TableBodyCell._tagDataType._DT_TEXT, name2);
							list2.Add(item);
						}
						else if (text.CompareTo("数量") == 0)
						{
							TableBodyCell item = new TableBodyCell(TableBodyCell._tagDataType._DT_TEXT, typeInfos.infosListByLevel[i].instanceCount.ToString());
							list2.Add(item);
						}
						else if (text.CompareTo("单位") == 0)
						{
							TableBodyCell item = new TableBodyCell(TableBodyCell._tagDataType._DT_TEXT, "樘");
							list2.Add(item);
						}
						else if (text.CompareTo("楼层") == 0)
						{
							TableBodyCell item = new TableBodyCell(TableBodyCell._tagDataType._DT_TEXT, typeInfos.infosListByLevel[i].levelName);
							list2.Add(item);
						}
						else
						{
							if (text.CompareTo("编号") == 0)
							{
								text = "类型标记";
							}
							Parameter parameter = familySymbol.GetParameter(text);
							if (parameter == null || !parameter.HasValue)
							{
								TableBodyCell item = new TableBodyCell(TableBodyCell._tagDataType._DT_TEXT, "");
								list2.Add(item);
							}
							else
							{
								string text2 = null;
                                switch ((int)parameter.StorageType)
								{
								case 1:
									text2 = AssistFunc.feetToMM((double)parameter.AsInteger()).ToString();
									break;
								case 2:
									text2 = AssistFunc.feetToMM(parameter.AsDouble()).ToString("f");
									break;
								case 3:
									text2 = parameter.AsString();
									break;
								case 4:
									text2 = parameter.AsElementId().IntegerValue.ToString();
									break;
								}
								TableBodyCell item;
								if (text2 != null)
								{
									item = new TableBodyCell(TableBodyCell._tagDataType._DT_TEXT, text2);
								}
								else
								{
									item = new TableBodyCell(TableBodyCell._tagDataType._DT_TEXT, "");
								}
								list2.Add(item);
							}
						}
					}
					TableBodyRow item2 = new TableBodyRow(list2);
					list.Add(item2);
				}
			}
			return list;
		}

		private class DoorWindowTypeInfosByLevel
		{
			public void AddInstanceCount()
			{
				this.instanceCount++;
			}

			public string levelName;

			public double levelElevation;

			public int instanceCount;
		}

		private class DoorWindowTypeInfos
		{
			private int FindLevelIndex(string levelName)
			{
				return this.infosListByLevel.FindIndex((DoorWindowDetailedStatementCreator.DoorWindowTypeInfosByLevel s) => s.levelName.CompareTo(levelName) == 0);
			}

			public void AddInfos(string levelName, double levelElevation)
			{
				if (this.infosListByLevel.Count < 1)
				{
					DoorWindowDetailedStatementCreator.DoorWindowTypeInfosByLevel doorWindowTypeInfosByLevel = new DoorWindowDetailedStatementCreator.DoorWindowTypeInfosByLevel();
					doorWindowTypeInfosByLevel.levelName = levelName;
					doorWindowTypeInfosByLevel.levelElevation = levelElevation;
					doorWindowTypeInfosByLevel.instanceCount = 1;
					this.infosListByLevel.Add(doorWindowTypeInfosByLevel);
					return;
				}
				int num = this.FindLevelIndex(levelName);
				if (num >= 0)
				{
					this.infosListByLevel[num].AddInstanceCount();
					return;
				}
				for (int i = 0; i < this.infosListByLevel.Count; i++)
				{
					if (Geometry.LessThan(levelElevation, this.infosListByLevel[i].levelElevation))
					{
						DoorWindowDetailedStatementCreator.DoorWindowTypeInfosByLevel doorWindowTypeInfosByLevel2 = new DoorWindowDetailedStatementCreator.DoorWindowTypeInfosByLevel();
						doorWindowTypeInfosByLevel2.levelName = levelName;
						doorWindowTypeInfosByLevel2.levelElevation = levelElevation;
						doorWindowTypeInfosByLevel2.instanceCount = 1;
						this.infosListByLevel.Insert(i, doorWindowTypeInfosByLevel2);
						return;
					}
				}
			}

			public ElementId typeId;

			public List<DoorWindowDetailedStatementCreator.DoorWindowTypeInfosByLevel> infosListByLevel;
		}
	}
}
